package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsAdminMapper;
import com.shop.mapper.DtsPermissionMapper;
import com.shop.mapper.DtsRoleMapper;
import com.shop.pojo.DtsAdmin;
import com.shop.pojo.DtsPermission;
import com.shop.pojo.DtsRole;
import com.shop.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DtsRoleServiceImpl
 * @Author xiao宋
 * @Date 20:18  2022/10/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    DtsRoleMapper dtsRoleMapper;
    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    DtsPermissionMapper permissionMapper;

    @Override
    public Set<String> findRolesById(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper();
        wrapper.in(DtsRole::getId, roleIds);
        wrapper.in(DtsRole::getDeleted, false);
        List<DtsRole> roleList = dtsRoleMapper.selectList(wrapper);
//        转成set数组
        Set<String> roles = roleList.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());
        return roles;
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole r = dtsRoleMapper.selectById(roleId);
        if (r.getName().equalsIgnoreCase("超级管理员")) {
            return true;
        }
        return false;
    }

    @Override
    public void delete(DtsRole role) {
        LambdaQueryWrapper<DtsAdmin> w = new LambdaQueryWrapper<>();
        w.select(DtsAdmin::getRoleIds);
        w.eq(DtsAdmin::getDeleted, false);
        List<DtsAdmin> adminList = adminMapper.selectList(w);
        for (DtsAdmin admin : adminList) {
            Integer[] rolesIds = admin.getRoleIds();
            for (Integer rolesId : rolesIds) {
                if (rolesId.equals(role.getId())) {
                    throw new RuntimeException("该角色被管理员关联不能删除。");
                }
            }
        }
//判断是否被关联权限表
        LambdaQueryWrapper<DtsPermission> pw = new LambdaQueryWrapper<>();
        pw.eq(DtsPermission::getRoleId, role.getId());
        pw.eq(DtsPermission::getDeleted, false);
        Integer count = permissionMapper.selectCount(pw);
        if (count > 0) {
            throw new RuntimeException("该角色被权限表关联，不能被删除。");
        }
        role.setDeleted(true);
        dtsRoleMapper.updateById(role);
    }


    @Override
    public void insert(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);
        dtsRoleMapper.insert(role);
    }

    @Override
    public void update(DtsRole role) {
        role.setUpdateTime(new Date());
        dtsRoleMapper.updateById(role);
    }

    @Override
    public List<DtsRole> findAll() {
        List<DtsRole>roleList=dtsRoleMapper.selectList(null);
        return roleList;
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole>iPage=new Page<>(page,limit);
        QueryWrapper<DtsRole>w=new QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)){
            w.like("name", rolename);
        }
        if ("desc".equalsIgnoreCase(order)){
            w.orderByDesc(sort);
        }else {
            w.orderByAsc(sort);
        }

        dtsRoleMapper.selectPage(iPage, w);
        Map<String,Object>data=new HashMap<>();
        data.put("items", iPage.getRecords());
        data.put("total", iPage.getTotal());
        return data;
    }
}
